package com.onetech.adrewards.free.activities;

import java.lang.reflect.Type;

import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.onetech.adrewards.free.objects.AdDTO;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.onetech.adrewards.free.R;

public class AdRewardDetailActivity extends BaseActivity {
	private TextView mContent=null;
	private ImageView mImageCover=null;
	private TextView mTitleAd=null;
	private TextView mChargeType=null;
	private TextView mAdPont=null;
	private TextView mCountTypeForUser=null; 
	private Button mDownloadBtn=null;
	private String mInstallUrl="";
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setMainView(R.layout.appli_detail_layout);
		showFooterView(false);
		showMyPoints(false);
		
		mContent=(TextView)findViewById(R.id.mainContent);
		mImageCover=(ImageView)findViewById(R.id.imageCover);
		mTitleAd=(TextView)findViewById(R.id.titleAd);
		mChargeType=(TextView)findViewById(R.id.chargeExp);
		mAdPont=(TextView)findViewById(R.id.adPoint);
		mCountTypeForUser=(TextView)findViewById(R.id.countTypeForUser);
		mDownloadBtn=(Button)findViewById(R.id.downloadBtn);
		
		if(getIntent()!=null){
			Bundle bundle=getIntent().getExtras();
			if(bundle!=null){
				try {
					String strData=bundle.getString("data");
					AdDTO data=deserializeInteger(strData);
					if(data!=null){
						mInstallUrl=data.getAd_tag_of_point_back();
						mTitleAd.setText(data.getContent_name());
						mChargeType.setText(AdrewadsUtils.getChargeTypeText(this.getApplicationContext(),data.getCharge_tpe()) + AdrewadsUtils.getAction_Cate_ID_Text(getApplicationContext(),data.getAction_cate_id()));
						mAdPont.setText(data.getMax_point()+ " pt");
						mContent.setText(Html.fromHtml(data.getContent_exp()));												
						mCountTypeForUser.setText(data.getCount_type_for_user());	

						ImageLoader.getInstance(this).DisplayImage(data.getBanner_img_1(), mImageCover);
					}					
				} catch (Exception e) {
					finish();
				}
			}else{
				finish();
			}			
		}else{
			finish();
		}
		
		mDownloadBtn.setOnClickListener(this);
	}	
	@Override
	public void onClick(View v) {
		int id=v.getId();
		switch (id) {
		case R.id.downloadBtn:
			AdrewadsUtils.go2Web(getApplicationContext(), mInstallUrl);
			break;

		default:
			super.onClick(v);
			break;
		}
	}
	private AdDTO deserializeInteger(String json) {
		AdDTO result = null;
		if (json == null || json.equals(""))
			return result;
		try {
			result = new AdDTO();
			Gson gson = new Gson();
			Type adDTO = new TypeToken<AdDTO>() {
			}.getType();
			result = (AdDTO) gson.fromJson(json, adDTO);
		} catch (Exception e) {
			return null;
		}
		return result;
	}
	@Override
	public void onStart() {
	    super.onStart();
	   // The rest of your onStart() code.
	    try {
	    	EasyTracker.getInstance(this).activityStart(this);  // Add this method.
		} catch (Exception e) {			
		}
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    // The rest of your onStop() code.
	    try {
	    	EasyTracker.getInstance(this).activityStop(this);  // Add this method.
		} catch (Exception e) {
		}
	  }
}
